import pandas as pd
import matplotlib.pyplot as plt

csv_file_name = '南玻A.csv'

# 可视化展示:
stock_data = pd.read_csv(csv_file_name, encoding='gb2312')  # 文件读取
stock_data = stock_data.sort_values(by=["日期"], ascending=[True], inplace=False)
stock_data_cleared = stock_data[stock_data['收盘价'] > 0]  # 数据清洗
stock_name = stock_data_cleared["名称"][0]
row_count = stock_data_cleared.shape[0]
xtick_interval = int(40)
xtick_index = range(row_count-365, row_count, xtick_interval)

plt.figure(figsize=(12, 6))

plt.rcParams['font.sans-serif'] = ['SimHei']  # 用来正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False  # 用来正常显示负号
plt.ion()
plt.show()
for i in range(row_count-365,row_count):
    plt.subplot(211)
    plt.plot(range(row_count-365,i),stock_data_cleared['收盘价'][row_count-365:i], color="#ff6666")
    #plt.xticks(xtick_index,stock_data_cleared.iloc[xtick_index,0],rotation=60)
    plt.xticks(xtick_index, "")
    #plt.yticks(range(0, 15, 5))
    #plt.xlabel("时间")
    plt.ylabel("收盘价")
    plt.grid(linewidth=0.5, color="#ff6666", alpha=0.5)
    plt.title(stock_name)
    plt.subplot(212)
    plt.bar(range(row_count-365, i), stock_data_cleared["成交量"][row_count-365:i], color="#008080")
    plt.xticks(xtick_index, stock_data_cleared.iloc[xtick_index, 0], rotation=15)
    #plt.yticks(range(0, 200000000, 20000000))
    plt.grid(linewidth=0.5, color="#008080", alpha=0.5)
    plt.subplots_adjust(hspace=0.1)
    plt.ylabel("成交量")
    plt.draw()
    plt.pause(0.00001)

